<template>
  <div class="app">
    <el-card style="width: 480px">
    <template #header>
      <div class="card-header">
        <span>Card name</span>
      </div>
    </template>
    <!-- 登录表单 -->
    <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="用户名" prop="name">
      <el-input v-model="ruleForm.name" />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>


    <template #footer>Footer content</template>
  </el-card>
  </div>
</template>
  
<script setup lang='ts'>
  import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
// import axios from 'axios'
import router from '@/router'
import {loginRequest} from '@/api/user'
import { ElMessage } from 'element-plus'
// const url = 'http://localhost:3001/admin/admin/login'


interface RuleForm {
  name: string
  password: string
  
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '',
  password: '',
  
})


const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 10, message: 'Length should be 3 to 10', trigger: 'blur' },
  ],
  password:[{
    pattern:/^.{5,}$/,
    message:'密码至少五位',
    required: true,
  }]
  
})
// interface IResData{
//     code:string,
//     message:string,
//     data: {
//         token:string,
//         adminname:string
//     },
// }



const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
        // 校验通过--》调用登录接口--》登录成功进行跳转
      console.log('submit!')
    //   axios.post(url,{adminname:ruleForm.name,password:ruleForm.password})
    //   .then((r)=>{console.log(r.data);
    //     router.push('/home')
    //   })
    const r = await loginRequest(ruleForm.name,ruleForm.password)
    console.log(r);

    // const data:IResData = r.data.data
    if(r.data.code == '200'){
        ElMessage({
        message: r.data.message,
        type: 'success',
        plain: true,
    })
        localStorage.setItem('xx-mall-admin-token',r.data.data.token)
        localStorage.setItem('xx-mall-admin-adminname',r.data.data.adminname)
        router.push('/home')
    }else{
        ElMessage({
        message: r.data.message,
        type: 'error',
        plain: true,
    })
    }
    
    } else {
        // fields---校验不通过，规则对象
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

// const userListAPI = 'http://localhost:3001/admin/admin/list'
// axios.get(userListAPI).then((r)=>{console.log(r);
// })


</script>
  
<style scoped lang="scss">
  .app{
    background-image: url('../assets/bg.jpg');
    height: 100%;
    display: flex;
    .el-card{
        margin: auto;
    }
  }
</style>